@attribute [Route(PageUrls.AddOrEditProduct + "/{Id:guid?}")]
@attribute [Route("{culture?}" + PageUrls.AddOrEditProduct + "/{Id:guid?}")]
@attribute [Authorize(Policy = AuthPolicies.PRIVILEGED_ACCESS)]
@attribute [Authorize(Policy = AppFeatures.AdminPanel.ManageProductCatalog)]
@inherits AppPageBase

<AppPageData ShowGoBackButton=true
             Title="@(Id == default ? @Localizer[nameof(AppStrings.AddProduct)] : @Localizer[nameof(AppStrings.EditProduct), product.Name!])" />

<section>
    @if (isLoading)
    {
        <BitStack VerticalAlign="BitAlignment.End" HorizontalAlign="BitAlignment.Center" Class="loading-stack">
            <BitEllipsisLoading />
        </BitStack>
    }
    else
    {
        <BitStack FillContent>
            <EditForm Model="product" OnValidSubmit="WrapHandled(Save)" novalidate>
                <AppDataAnnotationsValidator @ref="validatorRef" />

                <BitStack FillContent>
                    <BitTextField @bind-Value="product.Name"
                                  AutoComplete="@BitAutoCompleteValue.Off"
                                  Label="@Localizer[(nameof(AppStrings.Name))]"
                                  Placeholder="@Localizer[nameof(AppStrings.EnterProductName)]" />
                    <ValidationMessage For="() => product.Name" />

                    <BitDropdown @bind-Value="selectedCategoryId"
                                 Responsive
                                 Items="allCategoryList"
                                 Label="@Localizer[(nameof(AppStrings.Category))]"
                                 Placeholder="@Localizer[(nameof(AppStrings.SelectCategory))]"
                                 OnSelectItem="((BitDropdownItem<string> item) => { product.CategoryId = Guid.Parse(item.Value!); product.CategoryName = item.Text; })" />
                    <ValidationMessage For="@(() => product.CategoryId)" />

                    <BitNumberField @bind-Value="product.Price"
                                    Suffix="@CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol"
                                    NumberFormat="N"
                                    Label="@Localizer[(nameof(AppStrings.Price))]" />
                    <ValidationMessage For="() => product.Price" />

                    <BitRichTextEditor @ref=richTextEditorRef FullToolbar OnEditorReady="@(() => richTextEditorRef.SetHtml(product.DescriptionHTML))" />

                    <BitFileUpload @ref="fileUploadRef"
                                   AutoReset
                                   AutoUpload
                                   HideFileView
                                   Accept="image/*"
                                   Style="max-width:unset;"
                                   MaxSize="1024 * 1024 * 10"
                                   UploadUrlProvider="GetUploadUrl"
                                   OnUploading="() => isManagingFile = true"
                                   OnUploadFailed="HandleOnUploadFailed"
                                   OnUploadComplete="HandleOnUploadComplete"
                                   UploadRequestHttpHeadersProvider="WrapHandled(GetUploadRequestHeaders)">
                        <LabelTemplate>
                            <BitCard FullSize>
                                <BitStack HorizontalAlign="BitAlignment.Center">
                                    @if (isManagingFile is false)
                                    {
                                        <ProductImage Src="@product.GetPrimaryMediumImageUrl(AbsoluteServerAddress)" Alt="@product.PrimaryImageAltText" Width="50%" />

                                        @if (product.HasPrimaryImage)
                                        {
                                            <BitButton AutoLoading
                                                       Color="BitColor.Error"
                                                       ButtonType="BitButtonType.Button"
                                                       OnClick="WrapHandled(RemoveProductImage)">
                                                @Localizer[nameof(AppStrings.Remove)]
                                            </BitButton>

                                            <BitTextField @bind-Value="product.PrimaryImageAltText"
                                                          Multiline FullWidth AutoHeight
                                                          Label="@Localizer[nameof(AppStrings.AltText)]" />
                                            <ValidationMessage For="() => product.PrimaryImageAltText" />
                                        }

                                        <BitLink OnClick="() => fileUploadRef.Browse()" NoUnderline>
                                            <BitText>@Localizer[nameof(AppStrings.UploadNewImage)]</BitText>
                                        </BitLink>
                                    }
                                    else
                                    {
                                        <BitShimmer Shape="BitShimmerShape.Circle" Height="4.5rem" />
                                        <BitShimmer Shape="BitShimmerShape.Rectangle" Height="1.5rem" Width="12.5rem" />
                                    }
                                </BitStack>
                            </BitCard>
                        </LabelTemplate>
                    </BitFileUpload>

                    <BitStack Horizontal HorizontalAlign="BitAlignment.End">
                        <BitButton ButtonType="BitButtonType.Button" OnClick="GoBack" Variant="BitVariant.Outline">
                            @Localizer[nameof(AppStrings.Cancel)]
                        </BitButton>
                        <BitButton IsLoading=isSaving ButtonType="BitButtonType.Submit">
                            @Localizer[nameof(AppStrings.Save)]
                        </BitButton>
                    </BitStack>
                </BitStack>
            </EditForm>
        </BitStack>
    }
</section>